<?php
require_once(APP_LIB_DIR . 'MessagingModel.class.php');
include_once(APP_LIB_DIR . 'JSON.php');

Class DTRAction Extends Action
{
    function getDefaultView(&$controller, &$request, &$user)
    { 
        return VIEW_INPUT;
    }

    function getRequestMethods() { return REQ_POST | REQ_GET; }

    function IsSecure() { return false; }

    function get_config(){
        date_default_timezone_set('Asia/Manila');
        $mm = new MessagingModel('Messaging');
        $json   = new Services_JSON();
        $d = date("Y-m-d");
        $res =$mm->select_one("SELECT MAX(timelog) FROM dtr_log WHERE timelog LIKE '{$d}%';");
        $config = "";
        $config['date'] =($res[0]) ? date("Ymd", strtotime($res[0])) : date("Ymd");
        $config['time'] = ($res[0]) ? date("His", strtotime($res[0])) : "030000";
        return $json->encode($config);
    }

    function detokenize($template, $data){
        #"<date> Your child (<stud_name>/ <stud_no>) has EXITED the High School campus at <time>."
        $pattern = array('/<date>/i','/<stud_name>/i','/<stud_no>/i','/<time>/i');
        $replace = array($data['date'],$data['stud_name'],$data['stud_no'],$data['time']);
        return preg_replace($pattern, $replace, $template);
    }
    function send($data,$message){
        $mm = new MessagingModel('Messaging');
        $dt['message'] = $message;
        $dt['by']      = "dtr-system";
        $mid = $mm->insert('messages', $dt);

        $mm->send_group($data['msisdn'], $mid,false,$message,$data['id']);
    }

    # list || stud_no
    # smsc || optional
    # message || optional
    function execute(&$controller, &$request, &$user)
    {

        $ids = $request->getParameter('stud_no');
        $e_date = $request->getParameter('e_date');
        $e_time = $request->getParameter('e_time');

        if (!empty($ids)) { 
            $mm = new MessagingModel('Messaging');

            $smsc = $request->getParameter('smsc');
            $smsc = ($smsc=='auto') ? false : $smsc;

            $data = $mm->get_data($ids,date("Y-m-d",strtotime("$e_date$e_time")));
            if (!$data) {
                echo "DOES NOT EXIST";
                exit;
            }
            $data['date'] = date("Y-m-d",strtotime("$e_date$e_time"));
            $data['time'] = date("H:i:s",strtotime("$e_date$e_time"));

            if(!$data['datein']) {
                $dtr['datein'] = $data['date'];
                $dtr['timein'] = $data['time'];
                $dtr['member_id'] = $data['id'];
                $mm->insert('dtr',$dtr);
            }else{
                $dtr['dateout'] = $data['date'];
                $dtr['timeout'] = $data['time'];
                $mm->update('dtr',$dtr, "id='{$data['dtr_id']}'");
            }

            $dtrl['timelog'] = "{$data['date']} {$data['time']}";
            $dtrl['member_id'] = $data['id'];
            $mm->insert('dtr_log',$dtrl);

            switch($mm->get_config()):
                case "A":
                    $type= ($data['datein']) ? 'normal_out' : 'normal_in';
                    if ($template = $mm->get_template($data['time'],$type)){
                        $message = $this->detokenize($template['message'], $data);
                        $this->send($data,$message); 
                    }
                break;
            
                case "B":
                    $type= ($data['datein']) ? 'late_out' : 'late_in';
                    $template = $mm->get_template($data['time'],$type);
                    $template = ($template) ? $template : $mm->get_template($data['time'],'early_out');
                    if ($template){
                        $message = $this->detokenize($template['message'], $data);
                        $this->send($data,$message); 
                    }
                    
                break;
            
                case "C":
                    $type= ($data['datein']) ? 'out' : 'in';
                    $template = $mm->get_template($data['time'],$type);
                    if ($template){
                    $message = $this->detokenize($template['message'], $data);
                    $this->send($data,$message);
                    }
                break;
                default:
                echo "DISABLED";
                return VIEW_NONE;
            endswitch;
           
            echo "OK";
        } elseif ($request->getParameter('get_config')){
            echo $this->get_config();
        }else{
            echo "FAILED";
        }
        return VIEW_NONE;
    }
}
?>
